﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
//using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.DBUtility;


namespace mydddd.Web.sysModule
{
    public partial class Add : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                //初始绑定
                moduleIcon1.OnClientTriggerClick = Window1.GetSaveStateReference(moduleIcon1.ClientID, icon2.ClientID)
                   + Window1.GetShowReference("./SetIcon.aspx", "设置图标");

                BindTree();
                try
                {
                    if (Request["id"] != null) //修改
                    {

                        mydddd.BLL.SysModule bll = new mydddd.BLL.SysModule();
                        mydddd.Model.SysModule mo = bll.GetModulePathModel(int.Parse(Request["id"]));

                        parentID.Text = mo.parentID.ToString();
                        modulePath.Text = mo.modulePath;
                        moduleName.Text = mo.moduleName.ToString();
                        //moduleCode.Text = mo.moduleCode.ToString();
                        moduleUrl.Text = mo.moduleUrl.ToString();
                        moduleIcon1.Text = mo.moduleIcon.ToString();
                        sort.Text = mo.sort.ToString();
                        status.SelectedValue = mo.status.ToString();
                        info.Text = mo.info;

                        TreeNode tn = Tree1.FindNode(parentID.Text);
                        tn.Checked = true;

                        if (mo.moduleIcon.Length > 0)
                        {
                            icon2.Text = string.Format("<img src='../../{0}?{1}' />", mo.moduleIcon,
                                DateTime.Now.ToString("fff"));
                        }

                    }
                    else //添加
                    {
                        if (Request["pid"] == null || Request["pid"]=="0")
                        {
                            TreeNode tn = Tree1.FindNode("0");
                            tn.Checked = true;
                            parentID.Text = "0";
                            modulePath.Text ="";
                        }
                        else
                        {
                            TreeNode tn = Tree1.FindNode(Request["pid"]);
                            tn.Checked = true;

                            mydddd.BLL.SysModule bll = new mydddd.BLL.SysModule();
                            mydddd.Model.SysModule mo = bll.GetModel(int.Parse(Request["pid"]));
                            parentID.Text = mo.id.ToString();
                            modulePath.Text = mo.modulePath;
                        }
                    }
                }
                catch (Exception)
                {
                    Response.End();
                }
            }
        }

        protected void Button_save_OnClick(object sender, EventArgs e)
        {
            //验证
            TreeNode[] nodes = Tree1.GetCheckedNodes();
            if (nodes.Length == 0)
            {
                AlertWarning("请选择页面路径", false);
                return;
            }


            Dictionary<string, object> dic = new Dictionary<string, object>();

            dic["parentID"] = int.Parse(parentID.Text);
            dic["modulePath"] = modulePath.Text;
            dic["moduleName"] = moduleName.Text;
            //dic["moduleCode"] = string.Empty;
            dic["moduleUrl"] = moduleUrl.Text;
            dic["moduleIcon"] = moduleIcon1.Text;
            dic["sort"] = int.Parse(sort.Text);
            dic["status"] = int.Parse(status.SelectedValue);
            dic["info"] = info.Text;


            if (Request["id"] == null)//添加
            {
                int tempint = InsertDB("sysModule", dic);
                if (tempint > 0)
                {
                    //设置绝对路径
                    BLL.SysModule bll = new mydddd.BLL.SysModule();
                    bll.UpdateModulePath(tempint);

                    AlertInfor("添加成功", true);
                }
                else
                {
                    Alert.Show("添加失败");
                }
            }
            else//修改
            {
                Dictionary<string, object> dicWhere = new Dictionary<string, object>();

                dicWhere["id"] = int.Parse(Request["id"]);


                if (UpdateDB("sysModule", dic, dicWhere))
                {
                    //设置绝对路径
                    BLL.SysModule bll = new mydddd.BLL.SysModule();
                    bll.UpdateModulePath(int.Parse(Request["id"]));

                    AlertInfor("修改成功", true);
                }
                else
                {
                    Alert.Show("修改失败");
                }
            }
        }


        #region tree 方法

        //绑定所有分类
        private void BindTree()
        {

            BLL.SysModule moduleBll = new BLL.SysModule();

            string cv = string.Empty;//设置默认选中值

            if (Request["pid"] != null) //父节点
            {
                cv = Request["pid"];
            }

            CreateTree(Tree1,
                moduleBll.GetAllList(), "moduleName",
                "moduleIcon", "info", "status", cv);
        }

        /// <summary>
        /// 创建tree
        /// </summary>
        /// <param name="tree">tree控件ID</param>
        /// <param name="ds">数据集</param>
        /// <param name="nodeText">node显示的字段</param>
        /// <param name="nodeIcon">node显示的图标</param>
        /// <param name="nodeInfo">node显示的备注信息</param>
        /// <param name="show">是否显示</param>
        /// <param name="checkedValue">默认选中值</param>
        public static void CreateTree(FineUI.Tree tree, DataSet ds, string nodeText, string nodeIcon, string nodeInfo, string show, string checkedValue)
        {
            DataRow[] drParent = ds.Tables[0].Select("parentID=0");

            foreach (DataRow dr in drParent)
            {
                TreeNode node = AddNode(dr, nodeText, nodeIcon, nodeInfo, show, checkedValue);

                tree.Nodes.Add(node);
                ResolveSubTree(ds, dr["id"].ToString(), node, nodeText, nodeIcon, nodeInfo, show, checkedValue);
            }
        }

        /// <summary>
        /// 绑定tree节点node
        /// </summary>
        /// <param name="dr">数据源</param>
        /// <param name="nodeText">显示文本的字段</param>
        /// <param name="nodeIcon">显示图标的字段</param>
        /// <param name="nodeInfo">显示备注消息的字段</param>
        /// <param name="show">显示是否隐藏的字段</param>
        /// <param name="checkedValue">显示是否隐藏的字段</param>
        /// <returns></returns>
        private static TreeNode AddNode(DataRow dr, string nodeText, string nodeIcon, string nodeInfo, string show, string checkedValue)
        {
            TreeNode node = new TreeNode();
            node.NodeID = dr["id"].ToString();
            node.Text = dr[nodeText].ToString() + (dr[nodeInfo].ToString().Length > 0 ? "(" + dr[nodeInfo].ToString() + ")" : String.Empty) + (dr[show].ToString() == "1" ? String.Empty : "<font color='red'>不显示</font>");
            if (dr[nodeIcon].ToString().Length > 0)
                node.IconUrl = "/" + dr[nodeIcon].ToString();
            node.EnableCheckBox = true;
            node.EnableCheckEvent = true;
            //node.EnableClickEvent = true;//tree节点点击事件
            node.Expanded = true;//默认节点都为展开

            node.Checked = dr["id"].ToString() == checkedValue;


            return node;
        }

        /// <summary>
        /// 循环子节点
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <param name="parentID">父编号</param>
        /// <param name="nodeText">node显示的字段</param>
        /// <param name="nodeIcon">node显示的图标</param>
        /// <param name="nodeInfo">node显示的备注信息</param>
        /// <param name="show">是否显示</param>
        /// <param name="checkedValue">显示是否隐藏的字段</param>
        private static void ResolveSubTree(DataSet ds, string parentID, TreeNode treeNode, string nodeText, string nodeIcon, string nodeInfo, string show, string checkedValue)
        {
            DataRow[] drParent = ds.Tables[0].Select("parentID=" + parentID + "");

            foreach (DataRow dr in drParent)
            {
                TreeNode node = AddNode(dr, nodeText, nodeIcon, nodeInfo, show, checkedValue);

                treeNode.Nodes.Add(node);
                ResolveSubTree(ds, dr["id"].ToString(), node, nodeText, nodeIcon, nodeInfo, show, checkedValue);
            }
        }

        protected void Tree1_OnNodeCheck(object sender, TreeCheckEventArgs e)
        {
            string nodeid = string.Empty;
            //设置单选
            foreach (FineUI.TreeNode node in Tree1.GetCheckedNodes())
            {
                if (node.NodeID == e.Node.NodeID)
                {
                    node.Checked = true;
                    nodeid = node.NodeID;
                }
                else
                {
                    node.Checked = false;
                }
            }

            //设置父值
            if (nodeid.Length > 0 )
            {
                mydddd.BLL.SysModule bll = new mydddd.BLL.SysModule();
                mydddd.Model.SysModule mo = bll.GetModel(int.Parse(nodeid));
                if (mo != null)
                {
                    parentID.Text = mo.id.ToString();
                    modulePath.Text = mo.modulePath;
                }
                else
                {
                    parentID.Text = "0";
                    modulePath.Text = String.Empty;
                }
            }
            else
            {
                parentID.Text = "0";
                modulePath.Text = String.Empty;
            }
        }

        #endregion


    }
}